package com.szyjjk.controller;

import java.util.*;
import com.szyjjk.entity.*;
import com.szyjjk.service.RunPropertyServices;
import com.szyjjk.service.SysHearWordService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.common.collect.Maps;
import com.szyjjk.service.RunFilePathrService;
import com.szyjjk.service.RunMapPicService;
import com.szyjjk.util.DateUtil;

@Controller
@RequestMapping("/readMap")
public class MapPicController {

	@Value("${hydroPath}")
	private String hydroPath;
	@Value("${spareFilePath}")
	private String spareFilePath;
	@Value("${mapPicPath}")
	private String mapPicPath;
	@Value("${exeMapPicPath}")
	private String exeMapPicPath;


	@Autowired
	private SysHearWordService sysHearWordService;
	@Autowired
	private RunPropertyServices runPropertyServices;



	@Autowired
    private RunFilePathrService runFilePathrService;
	@Autowired
	private RunMapPicService runMapPicService;
	
	/**
	 * @Author yangyue
	 * @Description //TODO 返回map列表 和袁术列表
	 * @Date 11:40 2020/6/5
	 * @Param []
	 * @return java.util.Map<java.lang.String,java.lang.Object>
	 **/
	@RequestMapping(value = "/queryMap.do")
	@ResponseBody
	public  Map<String,Object> queryMap() {
		Map<String,Object>  map = Maps.newHashMap();
		try {
			List<RunFilePath> findFilePathByHz = runFilePathrService.findFilePathByHz("map");
			if(CollectionUtils.isNotEmpty(findFilePathByHz)) {
				for (RunFilePath runFilePath : findFilePathByHz) {
					String filePath = runFilePath.getFileRange();
						if(StringUtils.isNotBlank(filePath)) {
							String[] split = filePath.split("_");
							if(split.length==2) {
								//暂时封装 map范围数据 开始时间
								Date st = DateUtil.stringToDate2(split[0]);
								long time = st.getTime();
								runFilePath.setFilePath(time+"");
								
								
								Date end = DateUtil.stringToDate2(split[1]);
								int dayNum = DateUtil.getDayNum(st, end);
								int fileNo= (dayNum*24)+1;
								runFilePath.setUserId(fileNo+"");
							}	
						}
				}
			}
			
			map.put("date1", findFilePathByHz);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("date1", new ArrayList<>());
		}
		 return  map;
	}


	/**
	 * @Author yangyue
	 * @Description //TODO 返回图片
	 * @Date 11:40 2020/6/5
	 * @Param [mapId map的ID, code 元素的码, queryTime 精确到小时]
	 * @return com.szyjjk.entity.ResponseDataBo<com.szyjjk.entity.RunMapPic>
	 **/
	@RequestMapping(value = "/queryMapPic.do")
	@ResponseBody
	public  ResponseDataBo<RunMapPic> queryMapPic(String mapId,String code,String queryTime) {
		ResponseDataBo<RunMapPic> pb = new ResponseDataBo<RunMapPic>();
		if(StringUtils.isBlank(mapId)||StringUtils.isBlank(code)||StringUtils.isBlank(queryTime)) {
			pb.setCode(0);
			pb.setMessage("输入参数空");
			return pb;
		}
		RunProperty runProperty=runPropertyServices.findByIDPro(code);

		code=runProperty.getProRemark();
		//mapId="2b45e75a94a64b2aaac95eba5051a08c";
		//queryTime="2020-05-26 06:00:00";

		// 1 根据id查询
		RunFilePath rfp = runFilePathrService.findById(mapId);
		if(rfp==null) {
			pb.setCode(0);
			pb.setMessage("查询.map不存在");
			return pb;
		}
		Date date = new Date(Long.parseLong(queryTime));
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		//  2 查看该对应参数是否生成pic
		
	
		//时间换算
		String fileRange = rfp.getFileRange();
		if(StringUtils.isBlank(fileRange)) {
			pb.setCode(0);
			pb.setMessage("map记录的时间范围为空");
			return pb;
		}
		String[] split = fileRange.split("_");
		String touDate = split[0]+" 00:00:00";
		String weiDate =split[1]+" 00:00:00";
		Date touDateD = DateUtil.stringToDate3(touDate);
		Date weiDateD = DateUtil.stringToDate3(weiDate);
		long time = date.getTime();
		long touLong = touDateD.getTime();
		long weiLong = weiDateD.getTime();
		if(time>weiLong || time<touLong) {
			pb.setCode(0);
			pb.setMessage("输入的时间点不在该map的时间范围内");
			return pb;
		}
		List<RunMapPic> findFilePath = runMapPicService.findFilePath(rfp.getFileName(),code, calendar.getTime());
		if(CollectionUtils.isNotEmpty(findFilePath)) {
			RunMapPic runMapPic = findFilePath.get(0);
			pb.setEntity(runMapPic);
			return pb;
		}
		
		
		RunMapPic rmp = new RunMapPic();
		/*		
		rmp.setId(UUIDUtils.getUUID());
		rmp.setFileid(rfp.getId());
		rmp.setMapName(rfp.getFileName());
		rmp.setMapPath(rfp.getFilePath());
		rmp.setCcoPath(hydroPath+"com.cco");
		//  RunTest_bat.m 的全路径
		String mapParam=mapPicPath+"RunTest.m";
		rmp.setParamPath(mapParam);
		
		rmp.setMapValue(code);
		rmp.setMapCode(code);
		//计算 当前时间到头时间有多少值
		int dayNum = (DateUtil.getDayNum(touDateD, date));
		//整点时间
		String substring = queryTime.substring(11, 13);
		int num = (Integer.parseInt(substring))+1;
		int MapDate = (dayNum*24)+num;


		rmp.setMapDate(MapDate+"");     
		rmp.setMapTime(date);
	    rmp.setCreateTime(new Date());
	    String pic = DateUtil.getDate();
	    rmp.setMapPicName(pic+".jpg");
	    rmp.setMapPicPath(spareFilePath+"JPG\\");
		rmp.setRemake("0");        
		try {
			File f = new File(rmp.getMapPicPath());
			if(!f.exists()) {
				f.mkdirs();
			}
			
			String content = FileUtil.getFileContent6(spareFilePath+"RunTest.m");
			content = content.replace("test2_rPath", rfp.getFilePath()+rfp.getFileName());
			content = content.replace("ccoPath", rmp.getCcoPath());
			content = content.replace("attribute", rmp.getMapCode());
			content = content.replace("intTime", rmp.getMapDate());
			content = content.replace("jpgPath", rmp.getMapPicPath()+rmp.getMapPicName());
			FileUtil.createFileOrWrite(rmp.getParamPath(), content);
			String cmd =exeMapPicPath+"  "+rmp.getParamPath(); 
			Runtime rn = Runtime.getRuntime();
			Process p = rn.exec(cmd);
			p.getOutputStream().close();
			runMapPicService.insertRunFilePath(rmp);
			
			while(true) {
				Thread.sleep(1000L);
				File ff = new File(rmp.getMapPicPath()+rmp.getMapPicName());
				if(ff.exists()) {
					break;
				}
			}
			
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		pb.setCode(0);
		pb.setMessage("渲染数据生成中");
		pb.setEntity(rmp);
		return pb;
	}
	
}
